﻿using FluentValidation;
using Mes.Domain.BoundedContext;
using Microsoft.AspNetCore.Mvc;
using Mes.Application.Common;
using Mes.Application.DTO.Inputs.MasterData;
using Mes.Application.DTO.Results.MasterData;
using Mes.Application.Services.MasterData;

namespace Mes.Application.Web.Controllers.MasterData
{
    /// <summary>
    /// 物料清单控制器
    /// </summary>
    [ApiController]
    [Route("/api/v1")]
    public class MaterialBOMController : ControllerBase
    {
        private readonly IMaterialBOMService _materialBOMService;
        private readonly IValidator<AddMaterialBOMInput> _addMaterialBOMInputValidator;
        private readonly IValidator<GetMaterialBOMsInput> _getMaterialBOMsInputValidator;
        private readonly IValidator<UpdateMaterialBOMInput> _updateMaterialBOMInputValidator;

        /// <summary>
        /// 物料清单控制器
        /// </summary>
        /// <param name="materialBOMService"></param>
        /// <param name="addMaterialBOMInputValidator"></param>
        /// <param name="getMaterialBOMsInputValidator"></param>
        /// <param name="updateMaterialBOMInputValidator"></param>
        public MaterialBOMController(IMaterialBOMService materialBOMService,
            IValidator<AddMaterialBOMInput> addMaterialBOMInputValidator,
            IValidator<GetMaterialBOMsInput> getMaterialBOMsInputValidator,
            IValidator<UpdateMaterialBOMInput> updateMaterialBOMInputValidator
            )
        {
            _materialBOMService = materialBOMService;
            _addMaterialBOMInputValidator = addMaterialBOMInputValidator;
            _getMaterialBOMsInputValidator = getMaterialBOMsInputValidator;
            _updateMaterialBOMInputValidator = updateMaterialBOMInputValidator;
        }

        /// <summary>
        /// 新增物料清单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("materialbom")]
        public async Task<ActionResult> AddMaterialBOMAsync([FromBody] AddMaterialBOMInput input)
        {
            try
            {
                var result = await _addMaterialBOMInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }

                await _materialBOMService.AddMaterialBOMAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 编辑物料清单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPatch("materialbom")]
        public async Task<ActionResult> UpdateMaterialBOMAsync([FromBody] UpdateMaterialBOMInput input)
        {
            try
            {
                var result = await _updateMaterialBOMInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _materialBOMService.UpdateMaterialBOMAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除物料清单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("materialboms")]
        public async Task<ActionResult> DeleteMaterialBOMsAsync(IList<int> ids)
        {
            try
            {
                await _materialBOMService.DeleteMaterialBOMsAsync(ids);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 分页查询物料清单列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns>PagedItemsData<MaterialBOMResult></returns>
        [HttpGet("materialboms")]
        public async Task<ActionResult<PagedItemsData<MaterialBOMResult>>> GetMaterialsAsync([FromQuery] GetMaterialBOMsInput input)
        {
            try
            {
                var result = await _getMaterialBOMsInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                var response = await _materialBOMService.GetMaterialBOMsAsync(input);
                return Ok(response);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询物料清单实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns>MaterialBOMResult</returns>
        [HttpGet("materialbom")]
        public async Task<ActionResult<MaterialBOMResult>> GetMaterialsAsync(int id)
        {
            try
            {
                var response = await _materialBOMService.GetMaterialBOMAsync(id);
                return Ok(response);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 移除物料清单绑定的物料
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("materialofmaterialbom")]
        public async Task<ActionResult> RemoveMaterialOfMaterialBOMAsync(RemoveMaterialOfMaterialBOMInput input)
        {
            try
            {
                await _materialBOMService.RemoveMaterialOfMaterialBOMAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }
}
